package com.ruoyi.demo1.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.demo1.domain.Contacts;
import com.ruoyi.demo1.mapper.CustomersMapper;
import com.ruoyi.demo1.domain.Customers;
import com.ruoyi.demo1.service.ICustomersService;

/**
 * 客户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-18
 */
@Service
public class CustomersServiceImpl implements ICustomersService 
{
    @Autowired
    private CustomersMapper customersMapper;

    /**
     * 查询客户信息
     * 
     * @param customersId 客户信息主键
     * @return 客户信息
     */
    @Override
    public Customers selectCustomersByCustomersId(Long customersId)
    {
        return customersMapper.selectCustomersByCustomersId(customersId);
    }

    /**
     * 查询客户信息列表
     * 
     * @param customers 客户信息
     * @return 客户信息
     */
    @Override
    public List<Customers> selectCustomersList(Customers customers)
    {
        return customersMapper.selectCustomersList(customers);
    }

    /**
     * 新增客户信息
     * 
     * @param customers 客户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomers(Customers customers)
    {
        int rows = customersMapper.insertCustomers(customers);
        insertContacts(customers);
        return rows;
    }

    /**
     * 修改客户信息
     * 
     * @param customers 客户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomers(Customers customers)
    {
        customersMapper.deleteContactsByCustomersId(customers.getCustomersId());
        insertContacts(customers);
        return customersMapper.updateCustomers(customers);
    }

    /**
     * 批量删除客户信息
     * 
     * @param customersIds 需要删除的客户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomersByCustomersIds(Long[] customersIds)
    {
        customersMapper.deleteContactsByCustomersIds(customersIds);
        return customersMapper.deleteCustomersByCustomersIds(customersIds);
    }

    /**
     * 删除客户信息信息
     * 
     * @param customersId 客户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomersByCustomersId(Long customersId)
    {
        customersMapper.deleteContactsByCustomersId(customersId);
        return customersMapper.deleteCustomersByCustomersId(customersId);
    }

    /**
     * 新增联系人信息信息
     * 
     * @param customers 客户信息对象
     */
    public void insertContacts(Customers customers)
    {
        List<Contacts> contactsList = customers.getContactsList();
        Long customersId = customers.getCustomersId();
        if (StringUtils.isNotNull(contactsList))
        {
            List<Contacts> list = new ArrayList<Contacts>();
            for (Contacts contacts : contactsList)
            {
                contacts.setCustomersId(customersId);
                list.add(contacts);
            }
            if (list.size() > 0)
            {
                customersMapper.batchContacts(list);
            }
        }
    }
}
